<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
      <div @mouseenter="isEnterNav = true" @mouseleave="isEnterNav = false">
        <h2 class="all">全部商品分类</h2>
        <transition name="nav">
          <div class="sort" @mouseleave="leaveCategory" @mouseenter="isInCategory = true" @click="navToSearch"
            v-show="navShow">
            <div class="all-sort-list2">
              <div class="item" v-for="category1, index in categoryList" :key="category1.categoryId"
                @mouseenter="throttleFn(index)" :class="isEnter === index ? 'active' : ''">
                <h3>
                  <a href="javascript:;" :data-category1Id="category1.categoryId"
                    :data-categoryName="category1.categoryName">{{ category1.categoryName }}</a>
                </h3>
                <div class="item-list clearfix">
                  <div class="subitem">
                    <dl class="fore" v-for="category2 in category1.categoryChild" :key="category2.categoryId">
                      <dt>
                        <a href="javascript:;" :data-category2Id="category2.categoryId"
                          :data-categoryName="category2.categoryName">{{ category2.categoryName }}</a>
                      </dt>
                      <dd>
                        <em v-for="category3 in category2.categoryChild" :key="category3.categoryId">
                          <a href="javascript:;" :data-category3Id="category3.categoryId"
                            :data-categoryName="category3.categoryName">{{ category3.categoryName }}</a>
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
import { throttle } from "lodash"
import { mapState } from 'vuex';
export default {
  name: "TypeNav",
  data () {
    return {
      //鼠标移入到哪一个分类上
      isEnter: -1,
      //鼠标是否移入三级分类区域
      isInCategory: false,
      //鼠标是否移入整个nav区域
      isEnterNav: false
    }
  },
  mounted () {
    //三级分类列表需要多次使用，所以直接放在App组件中进行请求列表数据，避免不必要的请求
    // this.reqCategoryList()
    //在初始化的时候就把throttle处理完成并返回出来的真正要执行的函数赋值给占位函数，并且放在上方的mouseenter事件中调用
    this.throttleFn = throttle(this.mouseEnterItem, 500, {
      // 指定调用在节流开始前。
      leading: true,
      // 指定调用在节流结束后。
      trailing: true
    })
  },
  computed: {
    ...mapState("category", ["categoryList"]),
    navShow () {
      //只有在home页面的时候nav是显示的，其它页面默认隐藏，在鼠标移上去的时候显示
      if (this.$route.name === "home") return true
      return this.isEnterNav
    }
  },
  methods: {
    // ...mapActions("category", ["reqCategoryList"]),
    //鼠标移入三级分类列表的事件函数
    mouseEnterItem (index) {
      // console.log(index);
      if (!this.isInCategory) return
      this.isEnter = index
    },
    //设置一个throttle的返回值函数的占位
    throttleFn () { },
    //设置鼠标离开分类区域的事件函数
    leaveCategory () {
      this.isEnter = -1,
        this.isInCategory = false
    },
    //三级分类列表跳转至search页的事件函数
    navToSearch (e) {
      //先判断鼠标点击的元素是否是a标签
      if (e.target.nodeName !== "A") return
      //打印e.target.dataset发现a标签上的自定义属性名最终都变成了全小写，所以在下方获取的时候需要解构全小写的属性名
      // console.log(e.target.dataset);
      //编程式导航之前获取每个分类列表的id和name
      const { category1id, category2id, category3id, categoryname } = e.target.dataset
      // console.log(category1id, category2id, category3id, categoryname);
      const params = this.$route.params
      //编程式路由导航
      this.$router.push({
        name: "search",
        query: {
          category1Id: category1id,
          category2Id: category2id,
          category3Id: category3id,
          categoryName: categoryname
        },
        params
      })
    }
  },
  watch: {
    $route () {
      //在search中,我们点击nav中的某一项商品的时候,动态路由的query参数改变了,但是其他没变化,并且nav的窗口也没有隐藏
      //我们需要在点击过以后,让nav窗口隐藏
      this.isEnterNav = false
    }
  }
}
</script>

<style scoped lang="less">
.nav-enter,
.nav-leave-to {
  opacity: 0;
}

.nav-enter-active,
.nav-leave-active {
  transition: all .5s linear;
}

.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          &.active {
            h3 {
              background: red;
            }

            .item-list {
              display: block;
            }
          }

          // &:hover {
          //   .item-list {
          //     display: block;
          //   }
          // }
        }
      }
    }
  }
}
</style>